<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * ajax上传
 */
namespace Addon\AjaxUpload\Controller;
use Addon\AjaxUpload\Controller\CommonController;

/**
 * AjaxUpload上传插件 TODO上传其他类型文件
 */
class UploadController extends CommonController {
    
	//测试插件路径获得办法
	public function Test(){
		
		$url = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
		dump($url);
		$s = I();
		
		$ds = array_keys($s);
		dump(I());
		foreach($s as $i=>$k){
			
			$sd = $k;
		}
		dump($sd);
		dump($ds);
		
	}
	
	/**
	 * 上传图片
	 */
    public function Image(){

    	$upload = new \Org\Net\UploadFile();

    	$upload->autoSub = true;
    	
    	$upload->subType = 'custom';
		
		
		//设置上传目录
		$y = date('Y',time());
		$m = date('m',time());
		$d = date('d',time());
		
		$userid = getUID()?getUID():0;
		
		
		$rootPath = './upload/'.$userid.'/'.date('Y',time()).'/'.date('m',time()).'/'.date('d',time()).'/';
		$newrootPath = '/upload/'.$userid.'/'.date('Y',time()).'/'.date('m',time()).'/'.date('d',time()).'/';
		
		mkdirs($rootPath);
  
    	if ($upload->upload($rootPath)){

    		$info = $upload->getUploadFileInfo();

    	}
		
    	$file_newname = $info['0']['savename'];
	
    	
    	$MAX_SIZE = 20000000;
		//dump($info[0]['type']);
    	/* if($info['0']['type'] !='image/jpeg' && $info['0']['type'] !='text/plain' && $info['0']['type'] !='audio/mp3' && $info['0']['type'] !='image/jpeg' && $info['0']['type'] !='image/jpg' && $info['0']['type'] !='image/pjpeg' && $info['0']['type'] != 'image/png' && $info['0']['type'] != 'image/x-png'){
    		
            echo "2";

            exit;

    	} */
		
					// 资源统一管理
					$type = explode('.',$file_newname); 
			
					//设置编辑器公共上传路径
					$m_common_upload = M('common_upload');
					$m_common_upload_type = M('common_upload_type');
					
					//判断是否有类型没有用就新建
					
					$cutmap['title'] = $type[1];
					$cutdata = $m_common_upload_type->where($cutmap)->find();
					
					if($cutdata){
						
						$cuttype = $cutdata['id'];
						
					}else{
						
						$cutsdata['title'] = "$type[1]";
						$cutsdata['status'] = 1;
						$cuttype = $m_common_upload_type->add($cutsdata);
						
						
					}
					//判断用户
					$uid = $userid;
					//把上传内容存放入数据库
					$cudata =array(
						'status'=>1,
						'url'=> $newrootPath.$file_newname,
						'title'=>$info['0']['savename'],
						'original'=>$info['0']['name'],
						'type'=>$cuttype,
						'size'=>$info['0']['size'],
						'time'=>time(),
						'uid'=>$uid,
						
						);
					$m_common_upload->add($cudata);
					
					//资源统一管理
		
    	if($info['0']['size']>$MAX_SIZE)

    		echo "上传的文件大小超过了规定大小";
    	
    	if($info['0']['size'] == 0)

    		echo "请选择上传的文件";
    	
        	switch($info['0']['error']){

        		case 0:
        			echo $newrootPath.$file_newname;
				
        			break;

        		case 1:
        			echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
        			break;

        		case 2:
        			echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
        			break;

        		case 3:
        			echo "文件只有部分被上传";
        			break;

        		case 4:
        			echo "没有文件被上传";
        			break;

        	}
    }
}